import {Request} from "../../utils/request";
import host from "../../utils/host";
import func from "../../utils/functions";
import map from "../../utils/qqmap-wx-jssdk";
const HostUrl = host.service;
const Requ = new Request();
const Func = new func();
const Map = new map({
    key: 'KE4BZ-POLWP-XAHDB-LCLHX-IML6V-E6B7R'
});
var app = getApp();
var orderid
var text
var text1

Page({
    data: {
        UserInfo:{
            detailInfo: '请输入详情地址',
            userName: '请输入姓名',
            telNumber: '请输入电话',
        },
    },
    onLoad: function (options) {
        orderid = getApp().globalData.orderid;
		/* orderid = 2017057 */
        console.log(orderid)
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        var that = this
        this.setData({
            navH: app.globalData.navHeight,
        })
        // 支付订单详情
        new Request('3.php').request(
            () => {
                return {
                    order_id: orderid,
                    openid: app.globalData.openid,
                }
            },
            (res) => {
				console.log(res)
                wx.hideLoading()
                if (res.data.code == 0) {
                    wx.showToast({
                        title: '订单已过期',
                        icon: 'none',
                        duration: 2000,
                        mask: true
                    })
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 2000)
                } else {
                    let shopcar = res.data.shopcar;
                    for (let n = 0; n < shopcar.length; n++) {
                        for (let i = 0; i < shopcar[n].data.length; i++) {
                            if (shopcar[n].type == 1) {
                                shopcar[n].data[i].groupprice = (shopcar[n].data[i].num * shopcar[n].data[i].addprice).toFixed(2);
                                // 商品图片
                                // res.data.shopcar[n]['data'][i]['image'] = HostUrl.HostName + res.data.shopcar[n]['data'][i]['image'];
                                this.setData({
                                    groupprice: shopcar[n].data[i].groupprice,
                                });
                            }
                        }
                    }
                    text = res.data.text
                    text1 = res.data.text
                    if (res.data.code != 0) {
                        that.setData({
                            data: res.data,
                            shopCart: res.data.shopcar,
                            UserInfo: {
                                detailInfo: res.data.address,
                                userName: res.data.name,
                                telNumber: res.data.phone,
                            }
                        })
                    } else {
                        wx.showToast({
                            title: '订单已过期',
                            icon: 'none',
                            duration: 2000,
                            mask: true
                        })

                        setTimeout(function () {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 2000)
                    }
                }
            });
    },
    // 预览图片
    PreImage: function(e){
        Func.PreImage(e.currentTarget.dataset.image, e.currentTarget.dataset.image);
    },
    // 返回上一层
    goback: function (e) {
        setTimeout(function () {
            wx.navigateBack({
                delta: 1
            })
        }, 20);
    },
    // 去支付
    onpay: function () {
		new Request('pay_order.cls.php').request(
			() => {
				return {
					order_id: orderid,
					text: '',
					openid: app.globalData.openid,
					// U_id: app.globalData.U_id,
				}
			},
			(res) => {
				if(res.data.code == 2){
					Requ.showToast(res.data.msg, 'error');
					return;
				}
				wx.requestPayment({
					'timeStamp': res.data.timeStamp.toString(),
					'nonceStr': res.data.nonceStr,
					'package': res.data.package,
					'signType': 'MD5',
					'paySign': res.data.sign,
					'success': function (res) {
						wx.redirectTo({
							url: '/pages/order_details/order_details?orderid=' + orderid
						})
					},
					'fail': function (res) {
						wx.hideToast()
						wx.showToast({
							title: '支付已取消',
							icon: 'none',
							duration: 2000
						})
					}
				})
			});
    },
    // 订单备注
    remark: function (e) {
        if (text != e.detail.value) {
            wx.showModal({
                title: '提示',
                content: '是否保存备注？',
                success: function (res) {
                    if (res.confirm) {
                        wx.showLoading({
                            title: '正在保存',
                            mask: true
                        })
                        new Request('update_order_text.cls.php').request(
                            () => {
                                return {
                                    order_id: orderid,
                                    text: e.detail.value,
                                    openid: app.globalData.openid,
                                }
                            },
                            (res) => {
                                text = e.detail.value
                                text1 = e.detail.value
                                wx.hideLoading()
                                wx.showToast({
                                    title: '保存成功',
                                    icon: 'success',
                                    duration: 1000
                                })
                            });
                    } else if (res.cancel) {
                        this.setData({
                            text: text
                        })
                        text1 = 1323
                    }
                }
            })
        }
    },
    // 填写地址
    goselect: function (e) {
        let UserInfo = new Object();
        let address = Object;
        Func.GetAddress(UserInfo, (res, data)=>{
            this.setData({
                UserInfo: data,
            });
            address = {
                address0: "2", 
                phone0: data['telNumber'], 
                name0:  data['userName'], 
                addname0:  data['detailInfo'], 
                AddressDetail: data['provinceName']+data['cityName']+data['countyName']+data['detailInfo'], 
            };

            let PromiseId = new Promise((resolve, reject)=>{
                console.log(address.AddressDetail)
                //调用地址解析接口
                Map.geocoder({
                    //获取表单传入地址
                    address: address.AddressDetail, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
                    success: (res) => {//成功后的回调
                        resolve(res);
                    },
                    fail: (res)=>{ // 失败后的回调
                        console.log(123123123)
                        reject(res);
                    }
                });
            }); 

            PromiseId.then((res)=>{
                console.log(res);
                address.lat = res.result.location.lat;     // 纬度
                address.lng = res.result.location.lng;    // 经度
                new Request('update_order_address_info.cls.php').request(
                    () => {
                      return {
                        order_id: orderid,
                        openid: app.globalData.openid,
                        address: JSON.stringify(address),
                      }
                    },
                    (res) => {
                        res.data.code==1?Requ.showToast('修改成功'):Requ.showToast('修改失败');
                    }
                );
            },
            (res)=>{
                Requ.showToast('地址输入有误','error');
            });

            
        });
    },
	return:function(){
		wx.navigateBack({
			delta:1
		})
	}
})